/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.spring.provider.message.manager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;

import com.genia.toolbox.basics.i18n.I18nMessage;
import com.genia.toolbox.basics.i18n.SupportedLanguage;
import com.genia.toolbox.basics.process.Comparators;
import com.genia.toolbox.spring.provider.message.bean.MessageProvider;

/**
 * A custom implementation of {@link ResourceBundleMessageSource} that register
 * all bundled declared through {@link MessageProvider}.
 */
public class CustomResourceBundleMessageSource
    extends ResourceBundleMessageSource
{

  /**
   * The list of all {@link MessageProvider} to register.
   */
  private List<MessageProvider> messageProviders;

  /**
   * the {@link List} of supported languages.
   */
  private List<SupportedLanguage> supportedLanguages;



  /**
   * Try to resolve the message. Treat as an error if the message can't be
   * found.
   * 
   * @param message
   *          the nessage to resolve
   * @return the resolved message
   */
  public String getMessage(final I18nMessage message)
  {
    return getMessage(message, LocaleContextHolder.getLocale());
  }



  /**
   * Try to resolve the message. Treat as an error if the message can't be
   * found.
   * 
   * @param message
   *          the nessage to resolve
   * @param locale
   *          the Locale in which to do the lookup
   * @return the resolved message
   */
  public String getMessage(final I18nMessage message, final Locale locale)
  {
    return getMessage(message.getKey(), message.getParameters().toArray(), locale);
  }



  /**
   * The initialization method that register all {@link MessageProvider}.
   */
  public void init()
  {
    final List<String> basenames = new ArrayList<String>();
    for (final MessageProvider messageProvider : getMessageProviders()) {
      basenames.addAll(Arrays.asList(messageProvider.getBundledFiles()));
    }
    setBasenames(basenames.toArray(new String[basenames.size()]));
  }



  /**
   * getter for the messageProviders property.
   * 
   * @return the messageProviders
   */
  public List<MessageProvider> getMessageProviders()
  {
    return messageProviders;
  }



  /**
   * setter for the messageProviders property.
   * 
   * @param messageProviders
   *          the messageProviders to set
   */
  public void setMessageProviders(List<MessageProvider> messageProviders)
  {
    this.messageProviders = messageProviders;
  }



  /**
   * add a new {@link SupportedLanguage} to the set of languages supported by
   * the application.
   * 
   * @param supportedLanguage
   *          the language to add
   */
  public void addSupportedLanguage(SupportedLanguage supportedLanguage)
  {
    synchronized (supportedLanguages) {
      supportedLanguages.add(supportedLanguage);
      Collections.sort(supportedLanguages, Comparators.ORDERED_COMPARATOR);
    }
  }



  /**
   * remove a new {@link SupportedLanguage} to the set of languages supported by
   * the application.
   * 
   * @param supportedLanguage
   *          the language to remove
   */
  public void removeSupportedLanguage(SupportedLanguage supportedLanguage)
  {
    supportedLanguages.remove(supportedLanguage);
  }



  /**
   * getter for the supportedLanguages property.
   * 
   * @return the supportedLanguages
   */
  public List<SupportedLanguage> getSupportedLanguages()
  {
    return Collections.unmodifiableList(supportedLanguages);
  }



  /**
   * setter for the supportedLanguages property.
   * 
   * @param supportedLanguages
   *          the supportedLanguages to set
   */
  public void setSupportedLanguages(List<SupportedLanguage> supportedLanguages)
  {
    this.supportedLanguages = supportedLanguages;
  }

}
